import axios from 'axios';

interface ApiConfig{
    body:object;
    data:object;
    headers?: object;
}
const instance = axios.create({
    baseURL: 'http://t.weather.itboy.net',
    timeout: 800000,
    method:'POST',
    headers: {
        'X-Custom-Header': 'credit',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Access-Control-Allow-Origin': '*'
    },
    withCredentials: false, // default
    responseType: 'json', // 默认值
    responseEncoding: 'utf8', // 默认值
    maxBodyLength: 2000,
    maxContentLength: 2000,
});
instance.interceptors.request.use(config =>{
        console.info("config value is  ",config)
        return config;
    },(error) =>{
        console.info("error value is  ",error)
        return Promise.reject(error);
    }
)
instance.interceptors.response.use(response =>{
        console.info("response value is  ",response)
        return response;
    },(error) =>{
        console.info(" response error value is  ",error)
        return Promise.reject(error)
    }
)
const requestPost = async (url:string,options:ApiConfig)=>{
    const servicePost = axios.create({
        baseURL: 'https://t.weather.itboy.net',
        timeout: 10000,
        method:'POST',
        headers: {
            'X-Custom-Header': 'credit',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Access-Control-Allow-Origin': '*'
        },
        withCredentials: false, // default
        responseType: 'json', // 默认值
        responseEncoding: 'utf8', // 默认值
        maxBodyLength: 2000,
        maxContentLength: 2000,
    })
    servicePost.interceptors.request.use(config => {
        console.info("options.body value is ",options.body)
        if(options && options.body){
            config.data = options.body
        }
        return config;
    })
    servicePost.interceptors.response.use(response =>{
        return response.data;
    })
    return await servicePost(url, options);
}
const requestGet = async (url:string,options:ApiConfig)=>{
    const serviceGet = axios.create({
        baseURL: 'https://t.weather.itboy.net',
        timeout: 10000,
        method:'GET',
        headers: {
            'X-Custom-Header': 'credit',
            'Content-Type': 'application/text/html',
            'Access-Control-Allow-Origin': '*'
        },
        withCredentials: false, // default
        responseType: 'json', // 默认值
        responseEncoding: 'utf8', // 默认值
        maxBodyLength: 2000,
        maxContentLength: 2000,
    })
    serviceGet.interceptors.request.use(config => {
        console.info("options.body value is ",options.body)
        if(options && options.body){
            config.data = options.body
        }
        return config;
    })
    serviceGet.interceptors.response.use(response =>{
        return response.data;
    })
    return await serviceGet(url, options);
}
export  default {requestPost,requestGet}
